﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace CGF
{
    [CustomEditor(typeof(PrizeWheel))]
    public class PrizeWheelEditor : Editor
    {
        PrizeWheel prizeWheel;
        SerializedObject serialized;
        List<SerializedProperty> properties;
        List<SerializedProperty> pointerProperties;


        void OnEnable()
        {
            prizeWheel = (PrizeWheel)target;
            serialized = new SerializedObject(target);

            properties = new List<SerializedProperty>();
            properties.Add(serialized.FindProperty("accelDuration"));
            properties.Add(serialized.FindProperty("decelRounds"));
            properties.Add(serialized.FindProperty("decelDuration"));
            properties.Add(serialized.FindProperty("reverse"));
            properties.Add(serialized.FindProperty("freeRotate"));
            properties.Add(serialized.FindProperty("pointer"));

            pointerProperties = new List<SerializedProperty>();
            pointerProperties.Add(serialized.FindProperty("tableCount"));
            pointerProperties.Add(serialized.FindProperty("hitAngleOffset"));
            pointerProperties.Add(serialized.FindProperty("pointerSound"));
        }

        public override void OnInspectorGUI()
        {
            serialized.Update();
            EditorGUI.BeginChangeCheck();

            for (int i = 0; i < properties.Count; i++)
            {
                EditorGUILayout.PropertyField(properties[i], true);
            }
            if (prizeWheel.pointer)
            {
                EditorGUI.indentLevel++;
                for (int i = 0; i < pointerProperties.Count; i++)
                {
                    EditorGUILayout.PropertyField(pointerProperties[i], true);
                }
                EditorGUI.indentLevel--;
            }

            if (EditorGUI.EndChangeCheck())
            {
                serialized.ApplyModifiedProperties();
                EditorUtility.SetDirty(prizeWheel);
                AssetDatabase.SaveAssets();
            }
        }
    }
}